﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using NewsPublish.Model.Response;
using NewsPublish.Service;
using NewsPublish.Web.Models;

namespace NewsPublish.Web.Controllers
{
    public class HomeController : Controller
    {
        private NewsService _newsService;
        private BannerService _bannerService;

        public HomeController(NewsService newsService, BannerService bannerService)
        {
            this._newsService = newsService;
            this._bannerService = bannerService;
        }

        /// <summary>
        /// 前台 首页 页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            ViewData["Title"] = "首页";
            return View(_newsService.GetNewsClassifyList());
        }


        /// <summary>
        /// 获取banner集合并返回到页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetBanner()
        {
            return Json(_bannerService.GetBannerList());
        }


        /// <summary>
        /// 获取新闻数量
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetNewsCount()
        {
            return Json(_newsService.GetNewsCount(c => true));
        }


        /// <summary>
        /// 获取新闻列表的前几条数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetHomeNewsList()
        {

            return Json(_newsService.GetNewsList(c => true, 6));
        }

        /// <summary>
        /// 获取最新被评论的新闻前N条新闻
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetNewsCommentsList()
        {
            return Json(_newsService.GetNewCommentNewsList(c => true, 10));
        }

        /// <summary>
        /// 搜索新闻
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult SearchOneNews(string keyword)
        {
            if (string.IsNullOrEmpty(keyword))
            {
                return Json(new ResponseModel { code = 0, result = "请输入关键字" });
            }
            return Json(_newsService.GetSearchOneNews(c => c.Title.Contains(keyword.Trim())));
        }

        /// <summary>
        /// 错误页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Wrong()
        {
            ViewData["Title"] = "404";
            return View(_newsService.GetNewsClassifyList());
        }

    }
}
